home *** CD-ROM | disk | FTP | other *** search
/ Aminet 37 / Aminet 37 (2000)(Schatztruhe)[!][Jun 2000].iso / Aminet / comm / bbs / cit_src_AD08.lha / clean.c < prev    next >
C/C++ Source or Header  |  1998-06-13  |  6KB  |  281 lines

  1. /*
  2.  *                              Clean.c
  3.  *
  4.  * Cleanses a room of a user.
  5.  */
  6.  
  7. #include "ctdl.h"    /* header file  */
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include <ctype.h>
  13. #include <time.h>
  14. #include <proto/exec.h>
  15. #include <dos/dos.h>
  16. #include <pragmas/dos_pragmas.h>
  17. #include "exec/memory.h"
  18. #include "exec/ports.h"
  19. #include "exec/exec.h"
  20.  
  21. /*
  22.  *                              History
  23.  *
  24.  * 88Jun23 HAW  Created
  25.  */
  26.  
  27. /*
  28.  *                              Contents
  29.  *
  30.  *      crashout()              irrecoverable error
  31.  *      doRest()                Read msgs.
  32.  *      handle()                Inserts msg into room slot, etc.
  33.  *      indexRooms()            build RAM index to ctdlroom.sys
  34.  *      init()                  Open up files for recovery effort.
  35.  *      main()                  Master control.
  36.  *      noteRoom()              enter room into RAM index
  37.  */
  38.  
  39. extern CONFIG  cfg;             /* The main variable to be saved */
  40. extern MessageBuffer msgBuf;    /* The -sole- message buffer */
  41. extern FILE    *msgfl, *msgfl2; /* file descriptor for the msg file */
  42. FILE *netLog=stdout;
  43. extern rTable  *roomTab;        /* RAM index of rooms */
  44. extern aRoom   roomBuf;         /* room buffer */
  45. extern FILE    *roomfl;         /* file descriptor for rooms    */
  46. extern int     thisRoom;        /* room currently in roomBuf    */
  47.  
  48. struct bad {
  49.     label name;
  50.     struct bad *next;
  51. } base;
  52.  
  53. void handle(MSG_NUMBER mess);
  54. void getUtilString(char *prompt, char *buf, int lim);
  55. char init(int argc, char **argv);
  56. void doMsgs(void);
  57. int UtilGetch(void);
  58. int  mPrintf(char *format, ...) {return 0; }  /* stub to quiet the linker */
  59.  
  60. /*
  61.  * crashout()
  62.  *
  63.  * Irrecoverable error?  Crash out of the program.
  64.  */
  65. void crashout(str)
  66. char *str;
  67. {
  68.     exit(printf(str));
  69. }
  70.  
  71. /*
  72.  * doRest()
  73.  *
  74.  * This loops thru msg file until finished.
  75.  */
  76. void doMsgs()
  77. {
  78.     MSG_NUMBER message, firstMessage;
  79.     extern struct mBuf mFile1;
  80.  
  81.     startAt(msgfl, &mFile1, 0, 0);
  82.     getMessage(getMsgChar, FALSE, FALSE, TRUE);
  83.     message = atol(msgBuf.mbId);
  84.     printf("%ld\n", message);
  85.     firstMessage = message;
  86.     handle(message);
  87.     getMessage(getMsgChar, FALSE, FALSE, TRUE);
  88.     message = atol(msgBuf.mbId);
  89.     while (message != firstMessage) {
  90.         printf("%ld\n", message);
  91.         handle(message);
  92.         getMessage(getMsgChar, FALSE, FALSE, TRUE);
  93.         message = atol(msgBuf.mbId);
  94.     }
  95. }
  96.  
  97. /*
  98.  * handle()
  99.  *
  100.  * This function handles setting up and saving room indexes, etc.
  101.  */
  102. void handle(mess)
  103. MSG_NUMBER mess;
  104. {
  105.     int k, j;
  106.     struct bad *cur;
  107.  
  108.     if (strCmpU(msgBuf.mbroom, roomBuf.rbname) != SAMESTRING) return;
  109.     for (cur = base.next; cur != NULL; cur = cur->next)
  110.         if (strCmpU(cur->name, msgBuf.mbauth) == SAMESTRING) return;
  111.  
  112.     printf("Message from %s\n", msgBuf.mbauth);
  113.  
  114.     for (k = 0; roomBuf.msg[k+1].rbmsgNo < mess && k < MSGSPERRM-1; k++)
  115.         ;
  116.     for (j = 0; j < k; j++) {       /* Move msgs up 1.      */
  117.         roomBuf.msg[j].rbmsgLoc = roomBuf.msg[j+1].rbmsgLoc;
  118.         roomBuf.msg[j].rbmsgNo  = roomBuf.msg[j+1].rbmsgNo;
  119.     }
  120.     roomBuf.msg[k].rbmsgNo  = mess;/* And now insert msg    */
  121.     roomBuf.msg[k].rbmsgLoc = msgBuf.mbheadSector;
  122. }
  123.  
  124. /*
  125.  * init()
  126.  *
  127.  * This will set up the files for recovery.
  128.  */
  129. char init(argc, argv)
  130. int argc;
  131. char *argv[];
  132. {
  133.     label RoomName, UserName;
  134.     int  roomNo, count = 0, i;
  135.     SYS_FILE temp;
  136.     struct bad *cur;
  137.  
  138.     cfg.weAre = UTILITY;
  139.     if (!readSysTab(TRUE, TRUE)) crashout("\nDisaster!  I need CTDLTABL.SYS!");
  140.  
  141.     if (access(LOCKFILE, 0) != -1) {
  142.         printf("Please do not run Clean from Outside Commands.\n");
  143.         return FALSE;
  144.     }
  145.  
  146.     cfg.weAre = UTILITY;
  147.     mvToHomeDisk(&cfg.homeArea);
  148.     /* open message file */
  149.  
  150.     InitMsgBase();
  151.     makeSysName(temp, "ctdlroom.sys", &cfg.roomArea);
  152.     /* open room file */
  153.     openFile(temp, &roomfl);
  154.  
  155.     initRoomBuf(&roomBuf);
  156.  
  157.     if (argc != 2) {
  158.         getUtilString("room name", RoomName, NAMESIZE);
  159.     }
  160.     else strCpy(RoomName, argv[1]);
  161.  
  162.     printf("Check for room %s\n", RoomName);
  163.  
  164.     if ((roomNo = roomExists(RoomName)) == ERROR) crashout("No such room!");
  165.  
  166.     getRoom(roomNo);
  167.  
  168.     for (i = 0; i < MSGSPERRM; i++) {
  169.         roomBuf.msg[i].rbmsgNo = 0l;
  170.         roomBuf.msg[i].rbmsgLoc = 0;
  171.     }
  172.  
  173.     cur = &base;
  174.  
  175.     printf("Type in bad users names, end with no name:\n");
  176.     do {
  177.         getUtilString("", UserName, NAMESIZE);
  178.         if (strLen(UserName) != 0) {
  179.             count++;
  180.             cur->next = GetDynamic(sizeof *cur);
  181.             cur = cur->next;
  182.             strCpy(cur->name, UserName);
  183.             cur->next = NULL;
  184.         }
  185.     } while (strLen(UserName) != 0);
  186.  
  187.     if (count == 0) {
  188.         printf("No users specified!\n");
  189.         return FALSE;
  190.     }
  191.     return TRUE;
  192. }
  193.  
  194. /*
  195.  * main()
  196.  *
  197.  * This directs salvage proceedings and collects profits.
  198.  */
  199. int main(int, char **);
  200. int main(argc, argv)
  201. int argc;
  202. char **argv;
  203. {
  204.     printf("Citadel CLEAN a room %s\n%s\n", VERSION_NAME, COPYRIGHT);
  205.     if (init(argc, argv)) {
  206.         puts("No error msgs.  Go off and do something for an hour (or 2).\n");
  207.         doMsgs();
  208.         putRoom(thisRoom);
  209.         printf("Don't need to reconfigure.\n");
  210.     }
  211.     writeSysTab();
  212.     return 0;
  213. }
  214.  
  215. /*
  216.  * getUtilString()
  217.  *
  218.  * This function gets a string from the user.
  219.  */
  220. void getUtilString(prompt, buf, lim)
  221. char *prompt;
  222. char *buf;
  223. int  lim;       /* max # chars to read */
  224. {
  225.     char c;
  226.     int  i;
  227.  
  228.     if(strLen(prompt) > 0) {
  229.         printf("Enter %s\n : ", prompt, lim);
  230.     }
  231.  
  232.     i   = 0;
  233.     while (
  234.         c = UtilGetch(),
  235.  
  236.         c       != 13
  237.         && i     <  lim
  238.     ) {
  239.  
  240.         /* handle delete chars: */
  241.         if (c == BACKSPACE) {
  242.             putchar(' ');
  243.             putchar(BACKSPACE);
  244.             if (i > 0) i--;
  245.             else  {
  246.                 putchar(' ');
  247.                 putchar(BELL);
  248.             }
  249.         } else   buf[i++] = c;
  250.  
  251.         if (i >= lim) {
  252.             putchar(BELL);
  253.             putchar(BACKSPACE); i--;
  254.         }
  255.  
  256.     }
  257.     buf[i]  = '\0';
  258.     printf("\n");
  259. }
  260.  
  261. /*
  262.  * roomExists()
  263.  *
  264.  * This function returns slot# of named room else ERROR.
  265.  */
  266. int roomExists(room)
  267. char *room;
  268. {
  269.     int i;
  270.  
  271.     for (i = 0;  i < MAXROOMS;  i++) {
  272.         if (
  273.             roomTab[i].rtflags.INUSE == 1   &&
  274.             strCmpU(room, roomTab[i].rtname) == SAMESTRING
  275.         ) {
  276.             return(i);
  277.         }
  278.     }
  279.     return(ERROR);
  280. }
  281.